// @charset "utf-8";
define(function(require,exports,module){


	/*
		盘前引导模块

	 */
	var publicMethod = require('../../public/public');

	var template = require('../template/template'),
		chartModel = require('../model/chartmodel'),
		worldModule =require('../../smodules/world/init');


	var live = {
		sendLiveSay:function(page){ //看看直播大V正在说什么
			var self = this;
			page = page || chartModel['liveSay']['page'];

			//当page等于-1时那么需要请求数据
			if(page === -1){
				var url = 'http://t.10jqka.com.cn/api.php?method=newcircle.getZgCircleList&return=jsonp&callback=getcircle';
				$.support.cors = true;
				publicMethod.ajaxJsonp({
					type: 'GET',
					url: url,
					jsonpCallback:'getcircle',
					callback: function(result) {
						if(result){
							
							if(result['errorCode'] === 0){
								result = result['result'];
								
								//设置模型的相关属性
								chartModel['liveSay']['page'] = 0;
								chartModel['liveSay']['content'] = result;
								chartModel['liveSay']['length'] = result.length;
								
								if(chartModel['liveSay']['length'] >= 2){
									template.renderLiveSay(result[0],result[1]['name']);	
								}else{
									template.renderLiveSay(result[0],result[0]['name']);		
								}
								
								

							}
						}else{
							template.renderDefaultAnswer('网络异常！请检查');
						}
					}
					
				});

			}else{
				page = chartModel['liveSay']['page'];
				var length = chartModel['liveSay']['length'];
				//当page已经达到上限时，将page至0;
				
				if(page >= length){
					page = 0;
					chartModel['liveSay']['page'] = 0;
				}

				var nextName = '';
				if(page + 1 >= length){
					nextName = chartModel['liveSay']['content'][0]['name'];
				}else{
					nextName = chartModel['liveSay']['content'][page+1]['name'];
				}
				template.renderLiveSay(chartModel['liveSay']['content'][page],nextName);
			}
			

		},
		sendFamouslook:function(page){ //发送名家看盘请求
			var self = this;
			page = page || chartModel['famousLook']['page'];
			
			//当page等于-1时那么需要请求数据
			if( page === -1 ){
				$.support.cors = true;
				var url = 'http://master.10jqka.com.cn/mjdpcl_list/index.json';
				publicMethod.ajaxJson({
					type: 'GET',
					url: url,
					callback: function(result) {
						if(result){
							//chartModel['famousLook']['content'] = result['news'];
							
							//设置chartmodel中的值
							for(var key in result['news']){
								chartModel['famousLook']['content'].push(result['news'][key]);
							}
							chartModel['famousLook']['page'] = 0;
							chartModel['famousLook']['length'] = chartModel['famousLook']['content'].length;


							//拿数组中的第一个值去渲染页面
							template.renderFamouslook(chartModel['famousLook']['content'][0]);


						}else{
							template.renderDefaultAnswer('网络异常！请检查');
						}
					}
					
				});
			
			//当page不为-1，那么已经请求过数据，直接从chartmldel中取就可以了
			}else{
				var page = chartModel['famousLook']['page'];
				var length = chartModel['famousLook']['length'];
				//当page已经达到上限时，将page至0;
				if(page >= length){
					page = 0;
					chartModel['famousLook']['page'] = 0;
				}
				template.renderFamouslook(chartModel['famousLook']['content'][page]);
			}
			
		},
		sendVarious:function(page){ //渲染【各路观点】

			var self = this;
			
			page = page || chartModel['various']['page'];
			// page = -1;
			//当page等于-1时那么需要请求数据
			if( page === -1 ){
				
				$.support.cors = true;
				var url = 'http://stock.10jqka.com.cn/hsdp_list/index.json';
				publicMethod.ajaxJson({
					type: 'GET',
					url: url,
					callback: function(result) {
						if(result){
							
							//设置模型的相关属性
							chartModel['various']['page'] = 0;
							chartModel['various']['content'] = result;
							chartModel['various']['length'] = result.length;
							
							//拿数组中的第一个值去渲染页面
							template.renderVarious(result,chartModel['various']['page'],3);


						}else{
							template.renderDefaultAnswer('网络异常！请检查');
						}
					}
					
				});
			
			//当page不为-1，那么已经请求过数据，直接从chartmldel中取就可以了
			}else{
				page = chartModel['various']['page'];
				var length = chartModel['various']['length'];
				//当page已经达到上限时，将page至0;
				
				if(page >= length){
					page = 0;
					chartModel['various']['page'] = 0;
				}
				template.renderVarious(chartModel['various']['content'],page,3);
			}
		},
		sendSolution:function(page,len,txt){ //渲染【盘中解盘】
			var self = this;
			page = page || chartModel['solution']['page'];
			len = len || 3;
			
			//当page等于-1时那么需要请求数据
			if( page === -1 ){
				
				$.support.cors = true;
				var url = 'http://yuanchuang.10jqka.com.cn/djpingpan_list/index.json';
				publicMethod.ajaxJson({
					type: 'GET',
					url: url,
					callback: function(result) {
						if(result){
							
							//设置模型的相关属性
							chartModel['solution']['page'] = 0;
							chartModel['solution']['content'] = result;
							chartModel['solution']['length'] = result.length;
							//alert(chartModel['solution']['length']);
							//拿数组中的第一个值去渲染页面
							template.renderSolution(result,chartModel['solution']['page'],len,txt);


						}else{
							template.renderDefaultAnswer('网络异常！请检查');
						}
					}
					
				});
			
			//当page不为-1，那么已经请求过数据，直接从chartmldel中取就可以了
			}else{
				page = chartModel['solution']['page'];
				var length = chartModel['solution']['length'];
				//当page已经达到上限时，将page至0;
				
				if(page >= length){
					page = 0;
					chartModel['solution']['page'] = 0;
				}
				template.renderSolution(chartModel['solution']['content'],page,len,txt);
			}
		},
		sendGlobalMarket:function(){
			var data = new worldModule({callback: function (data) {
				var codeName = this.codeName;
				template.renderGlobalMarket(data,codeName);
			}});

		}
	};

	module.exports = live;

});

